//
//  CalculatorControl.m
//  DaDaBao
//
//  Created by admin on 15/8/14.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "CalculatorControl.h"


@interface CalculatorControl()
@property(nonatomic,strong)UILabel *inComeLab; //到期收益
@property(nonatomic,strong)UILabel *allLab;//利息共收入
@property(nonatomic,strong)UILabel *monthLab;//每月可收入
@property(nonatomic,strong)UILabel *inComeNumLab;//到期number
@property(nonatomic,strong)UILabel *allNumLab;//共收入number
@property(nonatomic,strong)UILabel *monthNumLab;//每月number

@end
@implementation CalculatorControl

//创建子视图
-(void)countMoneyWithRate:(NSString *)rate andTime:(NSString *)time
{
    self.backgroundColor = [UIColor colorWithRed:16.0f/255.0f green:21.0f/255.0f blue:29.0f/255.0f alpha:0.4];
//    白色底框
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(15.0f*WSCALE, 60.0f, SCREEN_WIDTH - 30.0f*WSCALE, 234.0f*HSCALE)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10.0f;
    [self addSubview:bgView];
//    标题
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(60.0f*WSCALE, 8.0f*HSCALE, bgView.frame.size.width-120.0f*WSCALE, 25.0f*HSCALE)];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.text = @"收益计算器";
    titleLab.font = [UIFont systemFontOfSize:18.0f*WSCALE];
    [bgView addSubview:titleLab];
    
//  退出按钮
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(bgView.frame.size.width-45.0f*WSCALE, 5.0f*WSCALE, 30.0f*WSCALE, 30.0f*WSCALE);
    [back setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(buttonMissTapped:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:back];
    
//    背景蓝色框
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(20.0f*WSCALE, titleLab.frame.size.height+titleLab.frame.origin. y+15.0f*HSCALE, bgView.frame.size.width-40.0f*WSCALE, 32.0f*HSCALE)];
    blueView.layer.cornerRadius = 5.0f*WSCALE;
    blueView.layer.borderWidth = 0.5f*WSCALE;
    blueView.layer.borderColor = [[UIColor hexStringToColor:COLOR_VALUE] CGColor];
    [bgView addSubview:blueView];
//   输入金额textfield
    UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(1.0f, 1.0f, bgView.frame.size.width-70.0f*WSCALE, 30.0f*HSCALE)];
    text.borderStyle = UITextBorderStyleNone;
    UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f*WSCALE, 28.0f*HSCALE)];
    UILabel *inputLab = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 0.0f, 100.0f*WSCALE, 28.0f*HSCALE)];
    inputLab.text = @"输入金额";
    inputLab.font = [UIFont systemFontOfSize:15.0f*WSCALE];
    [paddingView addSubview:inputLab];
    float font = 17.0f*HSCALE;
    text.font =[UIFont systemFontOfSize:font];
    text.leftView = paddingView;
    text.textAlignment = NSTextAlignmentRight;
    text.leftViewMode = UITextFieldViewModeAlways;
    [text addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    text.keyboardType = UIKeyboardTypeDecimalPad;
    text.delegate = self;

    [blueView addSubview:text];
   
    UILabel *yuanLab = [[UILabel alloc] initWithFrame:CGRectMake(text.frame.size.width+text.frame.origin.x+5.0f, text.frame.origin.y, 20.0f*WSCALE, inputLab.frame.size.height)];
    yuanLab.text = @"元";
    yuanLab.font = [UIFont systemFontOfSize:15.0f*WSCALE];
    [blueView addSubview:yuanLab];
    
    
//  到期收益
    _inComeNumLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, blueView.frame.size.height+blueView.frame.origin.y+15.0f*HSCALE, bgView.frame.size.width, 20.0f*HSCALE)];
    _inComeNumLab.textAlignment = NSTextAlignmentCenter;
    _inComeNumLab.textColor =[UIColor orangeColor];
    _inComeNumLab.font =[UIFont systemFontOfSize:20.0f*WSCALE];
    [bgView addSubview:_inComeNumLab];
    
    _inComeLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, _inComeNumLab.frame.size.height+_inComeNumLab.frame.origin.y+5.0f, bgView.frame.size.width, 20.0f*HSCALE)];
    _inComeLab.textAlignment = NSTextAlignmentCenter;
    _inComeLab.text = @"到期收益(本息收益/元)";
    _inComeLab.font = [UIFont systemFontOfSize:15.0f*WSCALE];
    
    [bgView addSubview:_inComeLab];
   
//    横分割线
    UILabel *horizontalLab = [[UILabel alloc] initWithFrame:CGRectMake(20.0f*WSCALE, _inComeLab.frame.size.height+_inComeLab.frame.origin.y+10.0f*HSCALE, bgView.frame.size.width-40.0f*WSCALE, 0.1f)];
    horizontalLab.backgroundColor = [UIColor blackColor];
    [bgView addSubview:horizontalLab];
    
//  利息共收入
    _allNumLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, horizontalLab.frame.size.height+horizontalLab.frame.origin.y+20.0f*HSCALE, bgView.frame.size.width/2, 20.0f*WSCALE)];
    _allNumLab.textAlignment = NSTextAlignmentCenter;
    _allNumLab.textColor =[UIColor orangeColor];
    _allNumLab.font =[UIFont systemFontOfSize:20.0f*WSCALE];
    [bgView addSubview:_allNumLab];
    
    _allLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, _allNumLab.frame.size.height+_allNumLab.frame.origin.y+5.0f*HSCALE, bgView.frame.size.width/2, 20.0f*HSCALE)];
    _allLab.textAlignment = NSTextAlignmentCenter;
    _allLab.text = @"利息共收入(元)";
    _allLab.font = [UIFont systemFontOfSize:14.0f*WSCALE];
    [bgView addSubview:_allLab];
    
    
//   纵向分割线
    UILabel *verticalLab = [[UILabel alloc] initWithFrame:CGRectMake(bgView.frame.size.width/2, horizontalLab.frame.size.height+horizontalLab.frame.origin.y+5.0f*HSCALE, 0.1f, 60.0f*HSCALE)];
    verticalLab.backgroundColor = [UIColor blackColor];
    [bgView addSubview:verticalLab];
    
//    平均每月可收入
    _monthNumLab = [[UILabel alloc] initWithFrame:CGRectMake(verticalLab.frame.origin.x+verticalLab.frame.size.width, _allNumLab.frame.origin.y, bgView.frame.size.width/2, 20.0f*HSCALE)];
    _monthNumLab.textAlignment = NSTextAlignmentCenter;
    _monthNumLab.textColor =[UIColor orangeColor];
    _monthNumLab.font =[UIFont systemFontOfSize:20.0f*WSCALE];
    [bgView addSubview:_monthNumLab];
    
    _monthLab = [[UILabel alloc] initWithFrame:CGRectMake(_monthNumLab.frame.origin.x,_allLab.frame.origin.y, bgView.frame.size.width/2, 20.0f*HSCALE)];
    
    _monthLab.textAlignment = NSTextAlignmentCenter;
    _monthLab.text = @"平均每月可收息(元)";
    _monthLab.font = [UIFont systemFontOfSize:14.0f*WSCALE];
    [bgView addSubview:_monthLab];
 
//    利率与期限
    self.rateValue = rate;
    self.productTime = time;

}

-(void)textChange:(UITextField *)textField
{
    
    float numValue = [textField.text floatValue];
    //    换算利润
    
    float inCome = numValue + numValue *[_rateValue floatValue]/100/360*[_productTime intValue];
    float all = inCome - numValue;
    float month = all/[_productTime intValue]*30;
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle =  NSNumberFormatterCurrencyStyle;
    NSString *str1 = [NSString stringWithFormat:@"%.2f",inCome];
    NSString *str2 = [NSString stringWithFormat:@"%.2f",all];
    NSString *str3 = [NSString stringWithFormat:@"%.2f",month];

    _inComeNumLab.text = [formatter stringFromNumber:[NSNumber numberWithFloat:[str1 floatValue]]];
    _allNumLab.text = [formatter stringFromNumber:[NSNumber numberWithFloat:[str2 floatValue]]];
    _monthNumLab.text = [formatter stringFromNumber:[NSNumber numberWithFloat:[str3 floatValue]]];

    
    if([textField.text floatValue] > 99999999.0f)
    {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"不能超过8位数" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil] show];
        
        
    }
    

}

//移除计算器
-(void)buttonMissTapped:(id)sender
{
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
